package com.learn.springcloud.hystrixexplainindetailconsumer.request.command.controller;

import com.learn.springcloud.hystrixexplainindetailconsumer.request.command.annotation.UserAnnotation;
import com.learn.springcloud.hystrixexplainindetailconsumer.request.command.command.UserCommand;
import com.learn.springcloud.hystrixexplainindetailconsumer.request.command.command.UserObservableCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import rx.functions.Action1;

import java.util.concurrent.Future;

/**
 * @Time : 2018/7/27 17:40
 * @Version : 1.0
 */
@RestController
public class CommandConsumer {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 同步调用--通过继承HystrixCommand
     * @param name
     * @return
     */
    @RequestMapping("/request/command/customise/say/hello/{name}")
    public String sayHello(@PathVariable String name){
        UserCommand userCommand = new UserCommand(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                5000,
                restTemplate,
                name);
        return userCommand.execute();
    }

    /**
     * 异步调用--通过继承HystrixCommand
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("/request/command/customise/say/hello/async/{name}")
    public String sayHelloAsync(@PathVariable String name) throws Exception{
        UserCommand userCommand = new UserCommand(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                5000,
                restTemplate,
                name);

        Future<String> futureString = userCommand.queue();
        return futureString.get();
    }

    /**
     * Hot Observable -- 通过继承HystrixCommand
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("/request/command/customise/say/hello/ho/{name}")
    public void sayHelloHo(@PathVariable String name) throws Exception{
        UserCommand userCommand = new UserCommand(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                5000,
                restTemplate,
                name);

        rx.Observable<String> observe = userCommand.observe();
        observe.subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        });
    }

    /**
     * Hot Observable -- 通过继承HystrixCommand
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("/request/command/customise/say/hello/co/{name}")
    public String sayHelloCo(@PathVariable String name) throws Exception{
        UserCommand userCommand = new UserCommand(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                5000,
                restTemplate,
                name);

        rx.Observable<String> observe = userCommand.toObservable();
        return observe.toString();
    }

    /**
     * 通过继承HystrixCommand
     * @param name
     * @return
     */
    @RequestMapping("/request/command/customise/say/hello/observable/{name}")
    public String sayHelloObservableCommand(@PathVariable String name){
        UserObservableCommand userObservableCommand = new UserObservableCommand(
                HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),
                restTemplate,
                name);
        return null;
    }

    @Autowired
    private UserAnnotation userAnnotation;

    /**
     * 同步调用--通过注解@HystrixCommand
     * @param name
     * @return
     */
    @RequestMapping("/request/command/customise/annotation/say/hello/{name}")
    public String sayHelloAnnotation(@PathVariable String name){
        return userAnnotation.sayHello(name);
    }
}
